﻿using JshRCSWebApi.Infrastructure;
using JshRCSWebApi.Infrastructure.Domain.DTO.SysService;
using JshRCSWebApi.Infrastructure.Domain.VO.SysService;
using JshRCSWebApi.Service.SysService;
using JshRCSWebApi.SqlSugarDB.Model.jshrcs_initial;
using SqlSugar;
using SqlSugar.Repository;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JshRCSWebApi.ServiceImpl.SysService
{
    public class RoleServiceImpl : RoleService
    {
        private readonly Repository<dynamic> _repository;

        public RoleServiceImpl(Repository<dynamic> repository)
        {
            this._repository = repository;
        }

        /// <summary>
        /// 新增角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult> AddRole(RoleAddDTO roleDTO, int UID)
        {
           var count = _repository.Add(new servicerole()
                                {
                                    srName = roleDTO.srName,
                                    srBriefIntroduction = roleDTO.srBriefIntroduction,
                                    srSDID = roleDTO.srSDID,
                                    srPassed = roleDTO.srPassed,
                                    CreateTime = DateTime.Now,
                                    CreateBy = UID
                                });
            return count > 0 ? await Task.Run(async() => ApiResult.Success("添加角色成功")) :
                await Task.Run(async () => ApiResult.Error("添加角色失败"));
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="iD"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult> DeleteRole(int ID)
             => _repository.Context.Deleteable<servicerole>().In(ID).ExecuteCommand() > 0 ?
                              await Task.Run(async () => ApiResult.Success("删除成功")) : await Task.Run(async () => ApiResult.Error("删除失败")); 

        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <param name="searchDTO"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<dynamic> GetRoleList(RoleSearchDTO searchDTO)
        {
            var dInfo = _repository.Select<servicedepartment>()
                                    .Where(d => d.sdName == searchDTO.sdName)
                                    .First();
            var sdId = dInfo?.ID ?? 0;
            int total = 0;
            var roleInfoList = _repository.Context.Queryable<servicerole>()
                        .WhereIF(sdId != 0, r => r.srSDID == sdId)
                        .WhereIF(!string.IsNullOrEmpty(searchDTO.srName), r => r.srName.Contains(searchDTO.srName))
                        .ToPageList(searchDTO.pageNo, searchDTO.pageSize,ref total);
            var roleList = new List<RoleInfoVO>();
            var dpListInfo =  _repository.SelectList<servicedepartment>();
            roleInfoList.ForEach(role =>
            {
               var sdInfo = dpListInfo.Where(d => role.srSDID == d.ID).First();
               var sdName = sdInfo?.sdName ?? string.Empty;
                roleList.Add(new RoleInfoVO()
                {
                    ID = role.ID,
                    sdName = sdName,
                    srName = role.srName,
                    srSDID = role.srSDID,
                    srPassed = role.srPassed,
                    srJurisdiction = role.srJurisdiction,
                    srBriefIntroduction = role.srBriefIntroduction,
                    CreateTime = role.CreateTime
                });
            });
            return await Task.Run(async () => new { roleList,total});
        }

        /// <summary>
        /// 获取角色名称和ID集合
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult> GetRoleNameAndIDList(int ID)
        {
              var roleInfoList = _repository.SelectList<servicerole>()
                                           .WhereIF(ID != 0, r => r.srSDID == ID)
                                           .Select(r => new { ID = r.ID, srName = r.srName })
                                           .ToList();
            return await Task.Run(async () => ApiResult.Success("查询成功", roleInfoList));
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="roleDTO"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult> UpdateRole(RoleUpdateDTO roleDTO, int UID)
        {
            var count = _repository.UpdateColumns(new servicerole()
                            {
                                srName = roleDTO.srName,
                                srBriefIntroduction = roleDTO.srBriefIntroduction,
                                srSDID = roleDTO.srSDID,
                                srPassed = roleDTO.srPassed,
                                ModifyBy = UID,
                                ModifyTime = DateTime.Now
                            },r => r.ID == roleDTO.ID,
                            new string[] { "srName", "srBriefIntroduction", "srSDID", "srPassed","ModifyBy", "ModifyTime" });
            return count > 0 ? await Task.Run(async() => ApiResult.Success("修改角色成功")) :
                    await Task.Run(async() => ApiResult.Error("修改角色失败"));
        }

        /// <summary>
        /// 修改角色权限
        /// </summary>
        /// <param name="updateDto"></param>
        /// <param name="v"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult> UpdateUserJurisdiction(RoleUpdateJurisdiction updateDto, int UID)
        {
            int count = _repository.UpdateColumns(new servicerole()
            {
                srJurisdiction = updateDto.StrJurisdiction,
                ModifyBy = UID,
                ModifyTime = DateTime.Now
            }, u => u.ID == updateDto.ID, new string[]
                                    {
                                        "srJurisdiction",
                                        "ModifyBy",
                                        "ModifyTime"
                                    });
            return await Task.Run(async () =>
              count > 0 ? ApiResult.Success("修改成功") : ApiResult.Error("修改失败"));
        }
    }
}
